"""
创建角色
"""
import logging

import pytest
from Config.Base import Base
from playwright.sync_api import Page, expect, Playwright
from Config.config import *
from Config.URL import *
import allure, os
from Config.Base import Base
from Config.Tool import tool


@pytest.mark.基础管理系统
@pytest.mark.组织权限
#@pytest.mark.parametrize('case', case_data)
def test_case(playwright : Playwright) -> None:
    SetupTeardown = Base(playwright)
    SetupTeardown.goto(url)
    SetupTeardown.login(username_admin, password_admin)
    context = SetupTeardown.context
    page = SetupTeardown.page
    page.goto(url+url_jichu_jueseguanli)
    page.get_by_placeholder("请输入用户名").fill(username_admin)
    page.get_by_placeholder("请输入密码").fill(password_admin)
    page.get_by_role("button", name="登 录").click()
    page.get_by_text("UAT-环北部湾广西水资源配置工程").first.click()
    page.get_by_text("组织权限 组织权限").click()
    page.get_by_role("link", name="ꘙ 角色管理").click()
    page.get_by_role("button", name=" 新建").click()
    page.get_by_label("新建").get_by_placeholder("请选择角色类型").click()
    page.get_by_role("listitem").nth(1).click()
    page.get_by_placeholder("请输入角色名称").fill("自动化测试角色名称")
    page.get_by_placeholder("请输入角色编码").fill("JSBM")
    page.get_by_label("新建").get_by_role("spinbutton").fill("01")
    page.get_by_role("button", name="确 定").click()
    expect(page.get_by_role("paragraph")).to_contain_text("新建成功！")
    page.get_by_placeholder("请输入名称").fill("自动化测试角色名称")
    page.get_by_role("textbox", name="请选择角色类型").click()
    page.get_by_role("listitem").nth(1).click()
    page.get_by_role("button", name=" 查询").click()
    # 获取tbody下面元素tr个数
    expect(page.locator("tbody > tr")).to_have_count(1)
    # 获取是否存在文本text
    expect(page.locator("tbody")).to_contain_text("自动化测试角色名称")

    page.get_by_role("button", name="删除").click()
    page.get_by_role("button", name="确 定").click()
    expect(page.get_by_role("alert")).to_contain_text("删除成功！")

    # 测试结果截图
    filename = os.path.join(test_screenshot_dir, f"{'创建角色'}.png")
    page.screenshot(path=filename)
    allure.attach.file(source=filename, name='创建角色',
                       attachment_type=allure.attachment_type.PNG)

    context.close()
    SetupTeardown.close()


